package com.sckj.springboot.hander;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


@Aspect
@Component
public class LogAspectHandler {

    //定义日志记录
    private final Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());

    /**
     * 定义一个切面，拦截com.itcodai.course09.controller包和子包下的所有方法
     */
    @Pointcut("execution(* com.sckj.springboot.controller.*.*(..))")
    public void pointCut(){}

    @Before("pointCut()")
    public void doBefor(JoinPoint joinPoint){
        logger.info("DoBefor方法进入");
        //获取签名
        Signature signature = joinPoint.getSignature();
        //获取切入包名
        String declaringTypeName = signature.getDeclaringTypeName();
        //获取即将执行的方法名
        String funName = signature.getName();
        logger.info("即将执行方法：{}，属于{}包", funName, declaringTypeName);

        // 也可以用来记录一些信息，比如获取请求的url和ip
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //获取请求url
        String url = request.getRequestURL().toString();
        //获取请求IP
        String ip = request.getRemoteAddr();
        logger.info("用户请求的url为：{}， ip为：{}", url, ip);
    }

    /**
     * 在上面定义的切面方法之后执行该方法
     * @param joinPoint jointPoint
     */
    @After("pointCut()")
    public void doAfter(JoinPoint joinPoint){
        logger.info("=====doAfter方法进入======");
        Signature signature = joinPoint.getSignature();
        String method = signature.getName();
        logger.info("方法{}以执行玩",method);
    }

    /**
     * 在上面定义的切面方法后执行该方法，可以捕获返回对象进行增强
     */
    @AfterReturning(pointcut = "pointCut()", returning = "result")
    public void doAfterReturning(JoinPoint joinPoint, Object result){
        Signature signature = joinPoint.getSignature();
        String classMethod = signature.getName();
        logger.info("方法{}执行完毕，返回参数为：{}",classMethod,result);
        //实际项目中可以根据业务做具体返回值增强
        logger.info("对返回参数进行业务上的增强:{}",result+"增强版");
    }

    /**
     * 上面定义切面方法执行抛异常时，执行该方法
     */
    @AfterThrowing(pointcut = "pointCut()",throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint,Throwable ex){
        Signature signature = joinPoint.getSignature();
        String method = signature.getName();
        //处理异常逻辑
        logger.info("执行方法{}出错，异常为：{}",method,ex);
    }
}
